const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon: 'fail',
    title: '',
    tip: '',
    list: [{
        icon: '/static/images/equipment/management/icon_002.png',
        title: '设备绑定科室',
        desc: '将设备绑定到本科室库中',
        type: 'equipmentBind'
      },
      {
        icon: '/static/images/equipment/management/icon_003.png',
        title: '损坏设备归还',
        desc: '强制将损坏的设备进行归还',
        // type: 'imperative'
        type: 'equipmentReturn'
      },
      // {
      //   icon: '/static/images/equipment/management/icon_004.png',
      //   title: '设备更换',
      //   desc: '将损坏设备的内容更换到新设备',
      //   type: 'replacement'
      // },
      {
        icon: '/static/images/equipment/management/icon_005.png',
        title: '逾期设备',
        desc: '查询已逾期未归还的设备',
        type: 'overdue'
      }
    ],
    notRented:0,
    renting:0,
    isLock:null
  },
  onShow(){
    this.setData({
      isLock:null
    })
    this.getList()
  },
  getList(){
    let that =this
    wx.request({
      url: app.globalData.serverUrl + '/medical/device/status',
            method: 'GET',
            header: {
              'token': wx.getStorageSync('token')
            },
            success:res=>{
              let resp = res.data
              if(resp.code==200){
                console.log(resp.data.deviceStatus);
                that.setData({
                  notRented:resp.data.deviceStatus.notRented,
                  renting:resp.data.deviceStatus.renting
                })
              }
            }
    })
  },
  bindtapConfirm() {
    let that = this
    that.selectComponent('#dialogImperative').hide()
    wx.navigateTo({
      url:"/pages/equipment/order/detail/going/going?type=equipmentReturn",
    })
  },
  /**
   * 用户点击取消
   */
  bindtapCancel() {
    this.selectComponent('#dialogImperative').hide()
  },
  bindtapScan(e) {
    const _this = this
    let lock=null
    if(!_this.data.isLock){
      _this.setData({
        isLock:new Date().getTime()
      })
    }else{
      lock = new Date().getTime()
    }
    if(_this.data.isLock && lock){
      if(lock-_this.data.isLock<app.globalData.isNumLock){
        return
      }else{
        _this.setData({
          isLock:lock
        })
      }
    }
    const type = e.currentTarget.dataset.type
    const urlMap = {
      'overdue': '/pages/equipment/management/overdue/list/list',
      "imperative": "/pages/equipment/order/detail/going/going",
      // "replacement": "/pages/equipment/order/detail/going/going",
      "equipmentReturn": "/pages/equipment/order/detail/going/going?type=equipmentReturn",
    }
    if (type === 'overdue') {
      return wx.navigateTo({
        url: urlMap[type] + '?type=' + type,
      })
    }
    if(type=='equipmentReturn'){
      this.selectComponent('#dialogImperative').show()
      return
    }
    wx.scanCode({
      success(res) {
        if (type == 'equipmentBind') {
          if(!res.result.match(RegExp(/便携式低频脉冲治疗仪/))){
            _this.showDialog({
              title: '无效二维码',
              icon: 'fail',
              tip: ['请扫描设备背部二维码']
            })
            return
          }
          let A = res.result.indexOf(';')
          let B = res.result.indexOf(';', A + 1)
          let deviceNo = res.result.substring(A + 1, B)
          wx.request({
            url: app.globalData.serverUrl + '/device/binding',
            method: 'POST',
            header: {
              'token': wx.getStorageSync('token')
            },
            data: {
              deviceNo
            },
            success: (res => {
              let resData = res.data
              if (resData.code == 200) {
                _this.showDialog({
                  title: '绑定成功',
                  icon: 'success',
                  tip: ['该设备已绑定到本科室']
                })
              } else {
                if(resData.message.indexOf(',')){
                  let arr = resData.message.split(',')
                  let arr1 = []
                  arr.map((item, index) => {
                    if (index != 0) {
                      arr1.push(item)
                    }
                  })
                  _this.showDialog({
                    title: arr[0],
                    icon: 'fail',
                    tip: [arr1]
                  })
                }else{
                  _this.showDialog({
                    title: resData.message,
                    icon: 'fail',
                    tip: ['']
                  })
                }
              }
            })
          })
        } else {
          wx.navigateTo({
            url: urlMap[type] + '?type=' + type,
          })
        }
      }
    })
  },
  showDialog({
    title,
    tip,
    icon
  } = {}) {
    this.setData({
      title: title,
      tip: tip,
      icon: icon
    })
    this.selectComponent('#dialog').show()
  },
})